import { pluginName } from '#auth'
import { addCard, delCard, getAllCard, getGroupCard, setCardDay, setGroupCard, setConfig, getConfig, setJsonAll } from '../models/index.js'
import schedule from "node-schedule"

const reg = new RegExp('^#?(生成卡密|卡密列表|授权列表|变更授权|转移授权|授权状态|清除授权|拉黑群|变更所有授权|不检查人数|不检查授权|添加白名单|移除白名单|全部白名单)\\s*(.*)?$')

schedule.scheduleJob('0 0 0 * * ?', () => {
    // 检查授权
    const list = getAllCard() || []
    for (const i of list) {
        if (i.status == 0) continue
        const card = i.card
        const day = i.day - 1
        if (day < 0) {
            delCard(card)
        } else {
            setCardDay(card, 1, 'del')
        }
    }
})

export class admin extends plugin {
    constructor() {
        super({
            name: `[${pluginName}] 主人相关`,
            dsc: `[${pluginName}] 主人相关`,
            event: 'message',
            priority: 1,
            rule: [
                {
                    reg,
                    fnc: 'admin',
                    permission: 'master'
                },
            ]
        })
    }

    async admin(e) {
        const regRet = reg.exec(e.msg)
        if (regRet) {
            const msg = []
            const node = []
            switch (regRet[1]) {
                case '生成卡密':
                    var [day, count] = regRet[2]?.split('*')
                    if (!day) {
                        msg.push('请输入天数')
                    } else {
                        addCard(Number(day), Number(count || 1))
                        msg.push('生成成功~')
                    }
                    break;
                case '卡密列表':
                    for (const i of getAllCard() || []) {
                        node.push({
                            message: `卡密: ${i.card}\n剩余天数: ${i.day}\n状态: ${i.status == 0 ? '未使用' : `已使用\n群: ${i.group_id}`}`
                        })
                    }
                    msg.push(await Bot.makeForwardMsg(node))
                    break
                case '授权列表':
                    for (const i of getAllCard() || []) {
                        if (i.status == 0) continue
                        node.push({
                            message: `卡密: ${i.card}\n剩余天数: ${i.day}\n状态: ${i.status == 0 ? '未使用' : `已使用\n群: ${i.group_id}`}`
                        })
                    }
                    msg.push(await Bot.makeForwardMsg(node))
                    break
                case '变更授权':
                    const bgsq = (new RegExp(/([0-9]+)([\+-]?)([0-9]+)?/g)).exec(regRet[2])
                    if (bgsq) {
                        const group_id = bgsq[1], type = bgsq[2], day = Number(bgsq[3])
                        const card = getGroupCard(group_id)?.card
                        if (card) {
                            if (!day) {
                                msg.push('请输入日期')
                            } else if (type === '+') {
                                setCardDay(card, day, 'add')
                                msg.push('操作成功')
                            } else if (type === '-') {
                                setCardDay(card, day, 'del')
                                msg.push('操作成功')
                            } else {
                                msg.push('请选择增加还是减少')
                            }
                        } else {
                            msg.push(`群:${group_id} 未找到授权`)
                        }
                    }
                    break
                case '转移授权':
                    if (regRet[2]) {
                        const [oldGroup, newGroup] = regRet[2].split('*')
                        if (!oldGroup || !newGroup) {
                            msg.push('缺少参数')
                        } else {
                            const oldCard = getGroupCard(oldGroup)?.card
                            const newCard = getGroupCard(newGroup)?.card
                            if (oldCard) {
                                if (!newCard) {
                                    setGroupCard(oldCard, oldGroup, 'del')
                                    setGroupCard(oldCard, newGroup, 'add')
                                    msg.push('操作成功')
                                } else {
                                    msg.push('目标群号还有授权')
                                }
                            } else {
                                msg.push('此群暂无授权')
                            }
                        }
                    }
                    break
                case '授权状态':
                    // TODO
                    break
                case '清除授权':
                    if (regRet[2]) {
                        const card = getGroupCard(regRet[2])?.card
                        if (card) {
                            setGroupCard(card, regRet[2], 'del')
                            msg.push('清除成功')
                        }
                    }
                    break
                case '拉黑群':
                    if (regRet[2]) {
                        setConfig(regRet[2], 'blackGroup', 'add')
                        msg.push('拉黑成功')
                    }
                    break
                case '变更所有授权':
                    if (regRet[2]) {
                        const day = Number(regRet[2])
                        if (!day) {
                            msg.push('输入不合法')
                        } else {
                            for (const i of getAllCard() || []) {
                                if (i.status == 0) continue
                                setCardDay(i.card, day, 'add')
                            }
                            msg.push('操作成功')
                        }
                    }
                    break
                case '不检查人数':
                    // TODO
                    break
                case '不检查授权':
                    // TODO
                    break
                case '添加白名单':
                    const add = Number(regRet[2] || e.group_id)
                    if (add) {
                        setConfig(add, 'whiteGroup', 'add')
                        msg.push('操作成功')
                    } else {
                        msg.push('请输入群号')
                    }
                    break
                case '移除白名单':
                    const del = Number(regRet[2] || e.group_id)
                    if (del) {
                        setConfig(del, 'whiteGroup', 'del')
                        msg.push('操作成功')
                    } else {
                        msg.push('请输入群号')
                    }
                    break
                case '全部白名单':
                    const whiteGroup = getConfig('whiteGroup')
                    if (whiteGroup) {
                        msg.push('白名单群:\n')
                        msg.push(whiteGroup.join('\n'))
                    } else {
                        msg.push('暂无白名单群')
                    }
                    break
                default:
                    break;
            }
            if (msg.length > 0) e.reply(msg)
        }
    }
}